import {Injectable} from '@angular/core';
import {Response} from "@angular/http";
import {ActivatedRoute, Router} from "@angular/router";
import {HttpService} from "@core/http/http.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import {Observable} from "rxjs";


import * as API from "@api/index";
import {
  CanReceiveCouponRespVO, CommonPageVo, ConditionRespVO, CouponRespVO, GoodsFilterResp, GoodsOrderRespVO, GoodsResp,
  GoodsRespVO,
  SNSResult
} from "@interface/index";
import {TicketDetailVO} from "../../interface/goods/goods.model";


@Injectable()
export class GoodsService {

  constructor(
    private httpService: HttpService,
    private tools: ToolsService,
    private router: Router
  ) {
  }

  /**
   * @description 商品详情接口
   * @author @chenwh576
   * @date 20170911
   * @type {string}
   */
  getGoodsDetail(params): Observable<SNSResult<GoodsRespVO>> {
    const param = this.tools.param(params);
    const url = API.GOODS_DETAIL + '?' + param;

    return this.httpService.get(url).map((res: Response) => res.json())
      .filter(item => item.result === '0');
  }

  toRouteData(route: ActivatedRoute) {
    return route.data;
  }

  fetchGoodsDetail(route: ActivatedRoute) {
    return this.toRouteData(route)
      .map(data => data.goodsDetails)
      .filter((item: SNSResult<GoodsRespVO>) => item.result === '0')
      .map((res) => res.data);
  }

  /**
   * @description 删除收藏
   * @author @chensk740
   * @date 2018-01-04
   */
  deleteFavorites(params) {
    const url = API.DELETE_FAVORITES + '?' + this.tools.param(params);
    return this.httpService.delete(url).map((res: Response) => res.json());
  }

  /**
   * @description 添加收藏
   * @author @chensk740
   * @date 2018-01-04
   */
  addFavorites(params) {
    const url = API.ADD_FAVORITES;
    return this.httpService.postUrlencode(url, params).map((res: Response) => res.json());
  }

  /**
   * @description 提交订单接口
   * @author @chenwh576
   * @date 20170915
   * @type {string}
   */
  sendOrderToConfirm(params): Observable<SNSResult<GoodsOrderRespVO>> {
    const param = this.tools.param(params);
    const url = API.ORDER_TO_CONFIRM + '?' + param;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description
   * @param 20035 您已经购买
   * @param 20058 该商品只能个人账号购买
   * @param result
   * @param msg
   * @param params
   */
  showGoodsResultMsg(result, msg, params) {
    const code = ["9008", "20046", "20044", "20034", "20032", "20035", "20058", "21078", "20057"];
    if (code.indexOf(result) > -1) {
      this.tools.showToast(msg);
    } else {
      const paramsUrl = this.tools.param(params);
      this.router.navigateByUrl(`/order/confirm-order?${paramsUrl}`, {queryParams: params});
    }
  }

  // 查詢訂單信息
  getOrderInfo(orderNumber: string) {
    return this.httpService.get(API.ORDER_INFO_URL + `?orderNumber=${orderNumber}`).map(res => res.json());
  }

  /**
   * @description 获取不同商品的FAQ
   * @author @zhupw1360
   * @date 2017-10-23
   * @type {string}
   */
  getGoodsFaq(faqkey: string) {
    const url = API.GET_GOODS_FAQ_URL + '?faqkey=' + faqkey;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  getGoodsList(params) {
    const url = API.GET_LIST_BY_CONDITION + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  getListFilter(cateCode: GoodsResp): Observable<SNSResult<GoodsFilterResp>> {
    const url = API.GET_GOODS_LIST_FILTER + `?cateCode=${cateCode}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 获取专题类型
   * @author ouyqx-691
   * @date 2017-11-25
   */
  getGoodsType() {
    const url = API.IP_AUTH_SERVICE;
    return this.httpService.get(url).map((res) => res.json());
  };


  /**
   * @description 获取轮播图
   * @author ouyqx-691
   * @date 2017-11-25
   */
  getSlider(topicId) {
    const url = API.TOPIC_BANNERS + '?deviceType=4&topicId=' + topicId;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 获取推荐商品布局信息
   * @author ouyqx-691
   * @date 2017-11-25
   */
  getGoodsModule(topicId: string) {
    const url = API.GOODS_MODULE + '?displayPosition=1&deviceType=4&topicId=' + topicId;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 获取推荐商品
   * @author ouyqx-691
   * @date 2017-11-25
   */
  getRecommendGoods(params) {
    const url = API.RECOMMEND_GOODS + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 可领取优惠券列表
   * @author zhupw-1360
   * @date 2018-01-02
   */
  getCouponList(params): Observable<SNSResult<CommonPageVo<CanReceiveCouponRespVO>>> {
    const url = API.GET_COUPON_CANRECEIVELIST + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 我的优惠券列表
   * @author zhupw-1360
   * @date 2018-01-02
   */
  getMyCouponList(params): Observable<SNSResult<CommonPageVo<CouponRespVO>>> {
    const url = API.GET_COUPON_MYLIST + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 可用优惠券列表
   * @author zhupw-1360
   * @date 2018-01-02
   */
  getUsableCouponList(params): Observable<SNSResult<CommonPageVo<CouponRespVO>>> {
    // console.log(params);
    const url = API.GET_COUPON_AVAILABLELIST + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 优惠券详情
   * @author zhupw-1360
   * @date 2018-01-02
   */
  getCouponDetail(codeId: number): Observable<SNSResult<CouponRespVO>> {
    const url = API.GET_COUPON_DETAIL + `?codeId=${codeId}`;
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 领取优惠券
   * @author zhupw-1360
   * @date 2018-01-02
   */
  postTakeCoupon(data) {
    const url = API.POST_COUPON_RECEIVE;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }

  /**
   * @description 使用兑换码获取优惠券
   * @author zhupw-1360
   * @date 2018-01-25
   */
  postCouponExchange(data) {
    const url = API.POST_COUPON_EXCHANGE;
    return this.httpService.postUrlencode(url, data).map((res) => res.json());
  }
  //
  // /**
  //  * @description 获取推荐商品
  //  * @author ouyqx691
  //  * @date 2017-09-11
  //  */
  // getRecommendGoods(params) {
  //   const url: string = API.SAME_GOODS_RECOMMEND + '?' + this.tools.param(params);
  //   return this.httpService.get(url).map((res) => res.json());
  // };


  /**
   * @description 获取票务服务列表
   * @author zhoukf1349
   * @date 2018-03-27
   */
  getGoodsTicketList(params): Observable<SNSResult<CommonPageVo<TicketDetailVO>>> {
    const url = API.TICKET_LIST + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 获取票务服务列表详情
   * @author zhoukf1349
   * @date 2018-03-27
   */
  getGoodsTicketDetail(goodsCode): Observable<SNSResult<CommonPageVo<TicketDetailVO>>> {
    const url = API.TICKET_DETAIL + `?goodsCode=${goodsCode}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 下单页面详情
   * @author zhoukf1349
   * @date 2018-04-08
   */
  getTicketSkuDetail(skuCode): Observable<SNSResult<CommonPageVo<TicketDetailVO>>> {
    const url = API.TICKET_GETSKUDETAIL + `?skuCode=${skuCode}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 下单页面详情
   * @author zhoukf1349
   * @date 2018-04-08
   */
  getTicketObtainType(goodsCode): Observable<SNSResult<CommonPageVo<TicketDetailVO>>> {
    const url = API.TICKET_GETOBTAINTYPE + `?goodsCode=${goodsCode}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }
  /**
   * @description 服务卡列表接口
   * @author chensk740
   * @date 2018-03-12
   * @param params
   */
  getServiceCardList(params: object): Observable<SNSResult<any >> {
    const url = API.SERVICE_CARD_LIST + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 服务卡列表页头部过滤类型列表
   * @author chensk740
   * @date 2018-03-12
   */
  getBusTypeList(): Observable<SNSResult<ConditionRespVO >> {
    const url = API.BUS_TYPE_LIST;
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 服务卡列表页头部过滤状态列表
   * @author chensk740
   * @date 2018-03-12
   */
  getListStatus(): Observable<SNSResult<ConditionRespVO >> {
    const url = API.STATUS_LIST;
    return this.httpService.get(url).map((res) => res.json());
  }

  /**
   * @description 获取推荐商品
   * @author ouyqx691
   * @date 2018-3-13
   */
  getServiceCardDetail(params) {
    const url: string = API.SERVICE_CARD_DETAIL + '?' + this.tools.param(params);
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 获取服务卡状态列表
   * @author ouyqx691
   * @date 2018-3-13
   */
  getServiceCardStatusList() {
    const url: string = API.SERVICE_CARD_STATUS_LIST;
    return this.httpService.get(url).map((res) => res.json());
  };

  /**
   * @description 下单页面详情
   * @author zhoukf1349
   * @date 2018-04-08
   */

  /*getTicketSkuDetail(skuCode):Observable<SNSResult<CommonPageVo<TicketDetailVO>>> {
    const url = API.TICKET_GETSKUDETAIL +  `?skuCode=${skuCode}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }*/
  /**
   * @description 提交订单
   * @author zhoukf1349
   * @date 2018-04-09
   */
  postTicketOrder(data: object) {
    const url = API.TICKET_CREATEORDER;
    return this.httpService.postUrlencode(url, data).map((res: Response) => res.json());
  }

  /**
   * @description 获取取票人信息
   * @author zhoukf1349
   * @date 2018-04-12
   */
  getTicketerInfo(orderNumber) {
    const url = API.TICKET_HOLDERINFO + `?orderNumber=${orderNumber}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }

  /**
   * @description 获取物流信息
   * @author zhoukf1349
   * @date 2018-04-12
   */
  getTicketerLogistics(orderNumber) {
    const url = API.TICKET_LOGISTICSINFO + `?orderNumber=${orderNumber}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }
  /**
   * @description 获取服务卡信息
   * @author zhoukf1349
   * @date 2018-04-16
   */
  getTicketServiceCardDetail(cardNo){
    const url = API.TICKET_CARDDETAIL  + `?cardNo=${cardNo}`;
    return this.httpService.get(url).map((res: Response) => res.json());
  }
}

